package com.gk.chat.utils;

import com.gk.chat.pojo.Users;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    private final StringRedisTemplate redisTemplate;

    public RedisUtil(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    //存储验证码（5分钟过期）
    public void setCode(String mobile, String code) {
        redisTemplate.opsForValue().set(mobile, code,5, TimeUnit.MINUTES);
    }

    //获取验证码
    public String getCode(String mobile) {
        return redisTemplate.opsForValue().get(mobile);
    }

    //删除验证码
    public void deleteCode(String mobile) {
        redisTemplate.delete(mobile);
        System.out.println("删除验证码成功");
    }

    //存储登录用户id
    public void setLoginUserId(String token, String userId) {
        String key="loginUser:"+token;
        redisTemplate.opsForValue().set(key, userId,24,TimeUnit.HOURS);
    }

    //获取登录用户id
    public String getLoginUserId(String token) {
        String key="loginUser:"+token;
        return redisTemplate.opsForValue().get(key);
    }

    //删除登录用户id
    public void deleteLoginUserId(String token) {
        String key="loginUser:"+token;
        redisTemplate.delete(key);
    }
}
